package com.lcxw.hxzbcommon.common;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    // TODO: 2020/7/4 依据redis命令修改名称   https://www.runoob.com/redis/redis-hashes.html
    // TODO: 2020/7/4 redis 连接数返回连接池
    public boolean hasKey(String key) {
        try {
            return stringRedisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 读取缓存
     *
     * @param key key
     * @return String
     */
    public String string_get(final String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 写入缓存
     */
    public boolean string_set(final String key, String value) {
        boolean result = false;
        try {
            stringRedisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 普通缓存放入并设置时间
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    public boolean string_set(String key, String value, long time) {
        try {
            if (time > 0) {
                stringRedisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                string_set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新缓存
     */
    public boolean string_getAndSet(final String key, String value) {
        boolean result = false;
        try {
            stringRedisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean string_delete(final String key) {
        boolean result = false;
        try {
            stringRedisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * redis SET中批量添加
     */
    public boolean set_add(String key, String... value) {
        boolean result = false;
        try {
            stringRedisTemplate.opsForSet().add(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取 Redis 中SET的值
     *
     * @return set
     */
    public Set<String> set_getMembers(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }


    /**
     * 获取redis 中 SET 的长度
     * <p>
     * size(k key)
     */
    public long set_getSize(String key) {

        return stringRedisTemplate.opsForSet().size(key);
    }


    /**
     * 检查给定的元素是否在变量中,true为存在
     * <p>
     * isMember(k key, object value)
     */
    public boolean set_existMember(String key, Object member) {
        return stringRedisTemplate.opsForSet().isMember(key, member);

    }

    /**
     * 转义变量的元素值到另一个变量中
     *
     * move(k key, v value, k targetKey)
     */
    public boolean set_move(String key,String member,String targetKey) {
        return stringRedisTemplate.opsForSet().move(key, member, targetKey);
    }

    /**
     * 批量删除变量中的元素,返回删除的数量
     *
     * remove(k key, v ...member)
     */
    public long set_remove(String key,Object... member) {
        return stringRedisTemplate.opsForSet().remove(key, member);
    }


    /**
     * 向集合最右边添加元素。如果key不存在会新建，添加成功返回添加后的总个数
     *
     * rightPush(K key, V value)
     */
    public void list_rightPush(String key,String member) {
        stringRedisTemplate.opsForList().rightPush(key, member);
    }

    /**
     * 获取集合中的size
     */
    public long list_getSize(String key){
        return stringRedisTemplate.opsForList().size(key);
    }

    /**
     * 获取集合中所有对象
     * @param key key
     * @return list
     */
    public List list_range(String key) {
        return stringRedisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 删除整个集合
     */
    public void list_delete(String key) {
        stringRedisTemplate.opsForList().getOperations().delete(key);
    }

    /**
     * 删除集合中等于member的所有元素
     */
    public void list_removeOne(String key,String member){
        stringRedisTemplate.opsForList().remove(key,0,member);
    }

    /*
     * 存入一个map类型的元素
     */
    public void map_put(String redisKey,String mapKey,Object mapValue){
        redisTemplate.opsForHash().put(redisKey,mapKey,mapValue);
    }

    /*
     * 获取map中所有键值对
     */
    public Map map_getAll(String redisKey){
        return redisTemplate.opsForHash().entries(redisKey);
    }

    /**
     * 获取redisKey中mapKey的值
     */
    public Object map_getOneValue(String redisKey,String mapKey){
        return redisTemplate.opsForHash().get(redisKey,mapKey);
    }

    /**
     * 删除一条
     */
    public Object map_deleteOne(String redisKey,String mapKey){
        return redisTemplate.opsForHash().delete(redisKey,mapKey);
    }

    /**
     * redis 中删除 map
     */
    public void map_deleteKey(String redisKey){
        redisTemplate.delete(redisKey);
    }
}